<?php
$pathToThis = pathinfo(__FILE__);
$ret = getcwd();
chdir($pathToThis['dirname']);

require_once("Report.class.php");

class SearchTable extends Report
{
	private $renderOptions = array();
	private $knownRenderOptions = array(
		'RenderSequence',
		'RenderIgnore',
		'RenderKeys',
		'CSS',
		'returnLinkA',
		'returnLinkOnClick',
		'IncludeOnHeader'
	);

	function __construct($fetchObject, $destroy = false, $cacheName = "SearchTable")
	{
		$this->fetchObject = $fetchObject;
		$this->cacheName = $cacheName;
		$this->getVar = "i";
		$this->flush();
	}

	public function render()
	{
		$lastPositionSequence = 0;
		if(isset($_GET[$this->getVar]))
			$this->start = $_GET[$this->getVar];
		else
			$this->start = 0;
		$this->fetchFromObject(); // Atualiza o buffer
		$this->total = $this->fetchObject->getControlTotal();
		$totalFetch = count($this->info);
		$ret = "";
		if(isset($this->renderOptions['IncludeOnHeader']))
			$ret .= $this->renderOptions['IncludeOnHeader'];
		$ret .= "<TABLE width='100%'><TR>";
		foreach($this->renderOptions['RenderSequence'] as $titulo)
		{
			if(!in_array($titulo, $this->renderOptions['RenderIgnore']))
				$ret .= '<TH align="left"><div>'.ucfirst($titulo).'</div></TH>';
		}
		$ret .= "</TR>";
		for($line = 0; $line < $totalFetch; $line++)
		{
			if(!isset($this->info[$line]))
				break;
			$ret .= '<TR><TD><div><A HREF="'.$this->parseRKey($this->renderOptions['returnLinkA'], $line).'"';
			if(isset($this->renderOptions['returnLinkOnClick']))
				$ret .= ' onClick="'.$this->parseRKey($this->renderOptions['returnLinkOnClick'], $line).'"';
			$ret .= '>'.$this->getInfoFromPosition($lastPositionSequence, $line).'</A></div></td>';
			
			while(($tmp = $this->getInfoFromPosition(++$lastPositionSequence, $line)) !== null)
				$ret .= '<td><div>'.$tmp.'</div></td>';
			$ret .= "</tr>";
			$lastPositionSequence = 0;
		}
		$ret .= "</TABLE>";
		// NAVEGACAO
		$ret .= '<table width="100%"><tr><td width="25%">';
		if($this->start != 0)
			$ret .= '<a href="?'.$this->getVar.'=0">&lt;&lt; Primeira</a>';
		else
			$ret .= '&lt;&lt; Primeira';
		$ret .= '</td><td width="25%">';
		if($this->start > 0)
			$ret .= '<a href="?'.$this->getVar.'='.$this->getPageAfter().'">&lt; Anterior</a>';
		else
			$ret .= '&lt; Anterior';
		$ret .= '</td><td width="25%" align="right">';
		if($this->start < $this->total && (($this->total - $this->start) > $this->pageElements))
			$ret .= '<a href="?'.$this->getVar.'='.$this->getEndValue().'">Pr&oacute;xima &gt;</a>';
		else
			$ret .= 'Pr&oacute;xima &gt;';
		$ret .= '</td><td width="25%" align="right">';
		if($this->start < ($this->total - $this->pageElements))
			$ret .= '<a href="?'.$this->getVar.'='.($this->total - $this->pageElements).'">&Uacute;ltima &gt;&gt;</a>';
		else
			$ret .= '&Uacute;ltima &gt;&gt;';
		$ret .= '</td></tr><tr><td colspan="4" align="center">Mostrando resultados de '.($this->start + 1).' at&eacute; '.$this->getEndValue().' em um total de '.$this->total.'</td></tr></table>';
		// /NAVEGACAO
		
		return $ret;
	}
	
	public function getPageAfter()
	{
		$tmp = $this->start - $this->pageElements;
		if($tmp < 0)
			$tmp = 0;
		return $tmp;
	}
	
	public function getEndValue()
	{
		$tmp = $this->start + $this->pageElements;
		if($tmp > $this->total)
			$tmp = $this->total;
		return $tmp;
	}
	
	private function parseRKey($struct, $line)
	{
		$totalKeys = count($this->renderOptions['RenderKeys']);
		for($i = 0; $i < $totalKeys; $i++)
		{
			$struct = preg_replace("/\[R_KEY".$i."\]/", $this->info[$line][$this->renderOptions['RenderKeys'][$i]], $struct);
		}
		return $struct;
	}
	
	public function getInfoFromPosition(&$id, $line)
	{
		if(!isset($this->renderOptions['RenderSequence'][$id]))
			return null;
		while(in_array($this->renderOptions['RenderSequence'][$id], $this->renderOptions['RenderIgnore']))
		{
			$id++;
			if(!isset($this->renderOptions['RenderSequence'][$id]))
				return null;
		}
		
		return $this->info[$line][$this->renderOptions['RenderSequence'][$id]];
	}

	public function setRenderOption($option, $value)
	{
		if(!in_array($option, $this->knownRenderOptions))
			throw new ReportException("Op&ccedil;&atilde;o '".$option."' desconhecida.");
		$this->renderOptions[$option] = $value;
	}
	public function getRenderOption($option)
	{
		if(!isset($this->renderOptions[$option]))
			return null;
		else
			return $this->renderOptions[$option];
	}
	public function setRenderDefaultOptions()
	{
		$this->renderOptions['RenderSequence'] = array();
		$this->renderOptions['RenderIgnore'] = array();
		$this->renderOptions['CSS'] = Array("table"=>"Table", "td"=>"TD", "tr"=>"TR");
		$this->renderOptions['returnLinkA'] = "";
		$this->renderOptions['returnLinkOnClick'] = "";
	}
	
	public function flush()
	{
		$this->pageElements = $this->fetchObject->getControlPage();
		$this->setRenderDefaultOptions();
	}
	
	public function resetStart()
	{
		$this->start = null;
	}
}

chdir($ret);
?>